home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / MuFS_crypt.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  34KB  |  1,058 lines

  1. RCS_ID_C="$Id: crypt.c,v 4.1 1994/05/16 14:09:07 ppessi Exp $";
  2. /*
  3.  * crypt.c --- password encryption
  4.  *
  5.  * This file is part of the AmiTCP/IP usergroup.library.
  6.  *
  7.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *
  10.  * Created      : Wed Sep 15 01:25:26 1993 ppessi
  11.  * Last modified: Mon May 16 01:56:59 1994 ppessi
  12.  */
  13.  
  14. /* static char sccsid[] = "@(#)crypt.c    5.11 (Berkeley) 6/25/91"; */
  15.  
  16. /*
  17.  * Copyright (c) 1989 The Regents of the University of California.
  18.  * All rights reserved.
  19.  *
  20.  * This code is derived from software contributed to Berkeley by
  21.  * Tom Truscott.
  22.  *
  23.  * Redistribution and use in source and binary forms, with or without
  24.  * modification, are permitted provided that the following conditions
  25.  * are met:
  26.  * 1. Redistributions of source code must retain the above copyright
  27.  *    notice, this list of conditions and the following disclaimer.
  28.  * 2. Redistributions in binary form must reproduce the above copyright
  29.  *    notice, this list of conditions and the following disclaimer in the
  30.  *    documentation and/or other materials provided with the distribution.
  31.  * 3. All advertising materials mentioning features or use of this software
  32.  *    must display the following acknowledgement:
  33.  *    This product includes software developed by the University of
  34.  *    California, Berkeley and its contributors.
  35.  * 4. Neither the name of the University nor the names of its contributors
  36.  *    may be used to endorse or promote products derived from this software
  37.  *    without specific prior written permission.
  38.  *
  39.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  40.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  43.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  45.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  48.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49.  * SUCH DAMAGE.
  50.  */
  51.  
  52.  
  53. /****** usergroup.library/crypt ********************************************
  54.  
  55.     NAME
  56.         crypt - password encryption with DES
  57.  
  58.     SYNOPSIS
  59.        result = crypt(key, setting);
  60.          D0           A0     A1
  61.  
  62.        char *crypt(const char *, const char *)
  63.  
  64.     FUNCTION
  65.         The crypt function performs password encryption.  It is derived from
  66.         the NBS Data Encryption Standard.  Additional code has been added to
  67.         deter key search attempts.  
  68.  
  69.     INPUTS
  70.         key - a NUL-terminated string (normally a password typed by a user).
  71.  
  72.         setting - a character array, 9 bytes in length, consisting of an
  73.                   underscore (`_') followed by 4 bytes of iteration count
  74.                   and 4 bytes of salt.  Both the iteration count and the
  75.                   salt are encoded with 6 bits per character, least
  76.                   significant bits first.  The values 0 to 63 are encoded by
  77.                   the characters `./0-9A-Za-z', respectively.
  78.  
  79.         The salt is used to induce disorder in to the DES algorithm in one
  80.         of 16777216 possible ways (specifically, if bit i of the salt is set
  81.         then bits i and i+24 are swapped in the DES `E' box output).  The
  82.         key is divided into groups of 8 characters (a short final group is
  83.         null-padded) and the low-order 7 bits of each each character (56
  84.         bits per group) are used to form the DES key as follows: the first
  85.         group of 56 bits becomes the initial DES key.  For each additional
  86.         group, the XOR of the group bits and the encryption of the DES key
  87.         with itself becomes the next DES key.  Then the final DES key is
  88.         used to perform count cumulative encryptions of a 64-bit constant.
  89.  
  90.     RESULTS
  91.         result - a NUL-terminated string, 20 bytes in length, consisting of
  92.                  the setting followed by the encoded 64-bit encryption.
  93.  
  94.     NOTE
  95.         For compatibility with Version 7 UNIX version of crypt(), the
  96.         setting may consist of 2 bytes of salt, encoded as above, in which
  97.         case an iteration count of 25 is used, fewer perturbations of DES
  98.         are available, at most 8 characters of key are used, and the
  99.         returned value is a NUL-terminated string 13 bytes in length.
  100.  
  101.     HISTORY
  102.         A rotor-based crypt() function appeared in Version 6 AT&T UNIX.  The
  103.         current style crypt() first appeared in Version 7 AT&T UNIX.
  104.  
  105.     BUGS
  106.         The crypt() function leaves its result in an internal static object
  107.         and returns a pointer to that object.  Subsequent calls to crypt()
  108.         will modify the same object.
  109.  
  110.     SEE ALSO
  111.         netutil/login, netutil/passwd, getpass(), netinfo.device/passwd
  112.  
  113.         Wayne Patterson, Mathematical Cryptology for Computer Scientists and
  114.         Mathematicians, ISBN 0-8476-7438-X, 1987.
  115.  
  116.         R. Morris, and Ken Thompson, "Password Security: A Case History",
  117.         Communications of the ACM, vol. 22, pp. 594-597, Nov. 1979.
  118.  
  119.         M.E. Hellman, "DES will be Totally Insecure within Ten Years", IEEE
  120.         Spectrum, vol. 16, pp. 32-39, July 1979.
  121.  
  122. ****************************************************************************
  123. */
  124.  
  125. #include <stdio.h>
  126. #include <string.h>
  127.  
  128. #include <clib/alib_protos.h>
  129.  
  130. #include <sys/errno.h>
  131. #include <assert.h>
  132.  
  133. #include "base.h"
  134. #include "libfunc.h"
  135.  
  136. #include <limits.h>
  137.  
  138. /*
  139.  * UNIX password, and DES, encryption.
  140.  * By Tom Truscott, trt@rti.rti.org,
  141.  * from algorithms by Robert W. Baldwin and James Gillogly.
  142.  *
  143.  * References:
  144.  * "Mathematical Cryptology for Computer Scientists and Mathematicians,"
  145.  * by Wayne Patterson, 1987, ISBN 0-8476-7438-X.
  146.  *
  147.  * "Password Security: A Case History," R. Morris and Ken Thompson,
  148.  * Communications of the ACM, vol. 22, pp. 594-597, Nov. 1979.
  149.  *
  150.  * "DES will be Totally Insecure within Ten Years," M.E. Hellman,
  151.  * IEEE Spectrum, vol. 16, pp. 32-39, July 1979.
  152.  */
  153.  
  154. /* =====  Configuration ==================== */
  155.  
  156. /*
  157.  * define "MUST_ALIGN" if your compiler cannot load/store
  158.  * long integers at arbitrary (e.g. odd) memory locations.
  159.  * (Either that or never pass unaligned addresses to des_cipher!)
  160.  */
  161. #if !defined(vax)
  162. #define    MUST_ALIGN
  163. #endif
  164.  
  165. #ifdef CHAR_BITS
  166. #if CHAR_BITS != 8
  167.     #error C_block structure assumes 8 bit characters
  168. #endif
  169. #endif
  170.  
  171. /*
  172.  * define "LONG_IS_32_BITS" only if sizeof(long)==4.
  173.  * This avoids use of bit fields (your compiler may be sloppy with them).
  174.  */
  175. #if !defined(cray)
  176. #define    LONG_IS_32_BITS
  177. #endif
  178.  
  179. /*
  180.  * define "B64" to be the declaration for a 64 bit integer.
  181.  * XXX this feature is currently unused, see "endian" comment below.
  182.  */
  183. #if defined(cray)
  184. #define    B64    long
  185. #endif
  186. #if defined(convex)
  187. #define    B64    long long
  188. #endif
  189.  
  190. /*
  191.  * define "LARGEDATA" to get faster permutations, by using about 72 kilobytes
  192.  * of lookup tables.  This speeds up des_setkey() and des_cipher(), but has
  193.  * little effect on crypt().
  194.  */
  195. #if defined(notdef)
  196. #define    LARGEDATA
  197. #endif
  198.  
  199. /* compile with "-DSTATIC" when profiling */
  200. #ifndef STATIC
  201. #define    STATIC    static
  202. #endif
  203.  
  204. #ifdef DEBUG
  205. STATIC void prtab(char *s, unsigned char *t, int num_rows);
  206. #endif
  207.  
  208. /* ==================================== */
  209.  
  210. /*
  211.  * Cipher-block representation (Bob Baldwin):
  212.  *
  213.  * DES operates on groups of 64 bits, numbered 1..64 (sigh).  One
  214.  * representation is to store one bit per byte in an array of bytes.  Bit N of
  215.  * the NBS spec is stored as the LSB of the Nth byte (index N-1) in the array.
  216.  * Another representation stores the 64 bits in 8 bytes, with bits 1..8 in the
  217.  * first byte, 9..16 in the second, and so on.  The DES spec apparently has
  218.  * bit 1 in the MSB of the first byte, but that is particularly noxious so we
  219.  * bit-reverse each byte so that bit 1 is the LSB of the first byte, bit 8 is
  220.  * the MSB of the first byte.  Specifically, the 64-bit input data and key are
  221.  * converted to LSB format, and the output 64-bit block is converted back into
  222.  * MSB format.
  223.  *
  224.  * DES operates internally on groups of 32 bits which are expanded to 48 bits
  225.  * by permutation E and shrunk back to 32 bits by the S boxes.  To speed up
  226.  * the computation, the expansion is applied only once, the expanded
  227.  * representation is maintained during the encryption, and a compression
  228.  * permutation is applied only at the end.  To speed up the S-box lookups,
  229.  * the 48 bits are maintained as eight 6 bit groups, one per byte, which
  230.  * directly feed the eight S-boxes.  Within each byte, the 6 bits are the
  231.  * most significant ones.  The low two bits of each byte are zero.  (Thus,
  232.  * bit 1 of the 48 bit E expansion is stored as the "4"-valued bit of the
  233.  * first byte in the eight byte representation, bit 2 of the 48 bit value is
  234.  * the "8"-valued bit, and so on.)  In fact, a combined "SPE"-box lookup is
  235.  * used, in which the output is the 64 bit result of an S-box lookup which
  236.  * has been permuted by P and expanded by E, and is ready for use in the next
  237.  * iteration.  Two 32-bit wide tables, SPE[0] and SPE[1], are used for this
  238.  * lookup.  Since each byte in the 48 bit path is a multiple of four, indexed
  239.  * lookup of SPE[0] and SPE[1] is simple and fast.  The key schedule and
  240.  * "salt" are also converted to this 8*(6+2) format.  The SPE table size is
  241.  * 8*64*8 = 4K bytes.
  242.  *
  243.  * To speed up bit-parallel operations (such as XOR), the 8 byte
  244.  * representation is "union"ed with 32 bit values "i0" and "i1", and, on
  245.  * machines which support it, a 64 bit value "b64".  This data structure,
  246.  * "C_block", has two problems.  First, alignment restrictions must be
  247.  * honored.  Second, the byte-order (e.g. little-endian or big-endian) of
  248.  * the architecture becomes visible.
  249.  *
  250.  * The byte-order problem is unfortunate, since on the one hand it is good
  251.  * to have a machine-independent C_block representation (bits 1..8 in the
  252.  * first byte, etc.), and on the other hand it is good for the LSB of the
  253.  * first byte to be the LSB of i0.  We cannot have both these things, so we
  254.  * currently use the "little-endian" representation and avoid any multi-byte
  255.  * operations that depend on byte order.  This largely precludes use of the
  256.  * 64-bit datatype since the relative order of i0 and i1 are unknown.  It
  257.  * also inhibits grouping the SPE table to look up 12 bits at a time.  (The
  258.  * 12 bits can be stored in a 16-bit field with 3 low-order zeroes and 1
  259.  * high-order zero, providing fast indexing into a 64-bit wide SPE.)  On the
  260.  * other hand, 64-bit datatypes are currently rare, and a 12-bit SPE lookup
  261.  * requires a 128 kilobyte table, so perhaps this is not a big loss.
  262.  *
  263.  * Permutation representation (Jim Gillogly):
  264.  *
  265.  * A transformation is defined by its effect on each of the 8 bytes of the
  266.  * 64-bit input.  For each byte we give a 64-bit output that has the bits in
  267.  * the input distributed appropriately.  The transformation is then the OR
  268.  * of the 8 sets of 64-bits.  This uses 8*256*8 = 16K bytes of storage for
  269.  * each transformation.  Unless LARGEDATA is defined, however, a more compact
  270.  * table is used which looks up 16 4-bit "chunks" rather than 8 8-bit chunks.
  271.  * The smaller table uses 16*16*8 = 2K bytes for each transformation.  This
  272.  * is slower but tolerable, particularly for password encryption in which
  273.  * the SPE transformation is iterated many times.  The small tables total 9K
  274.  * bytes, the large tables total 72K bytes.
  275.  *
  276.  * The transformations used are:
  277.  * IE3264: MSB->LSB conversion, initial permutation, and expansion.
  278.  *    This is done by collecting the 32 even-numbered bits and applying
  279.  *    a 32->64 bit transformation, and then collecting the 32 odd-numbered
  280.  *    bits and applying the same transformation.  Since there are only
  281.  *    32 input bits, the IE3264 transformation table is half the size of
  282.  *    the usual table.
  283.  * CF6464: Compression, final permutation, and LSB->MSB conversion.
  284.  *    This is done by two trivial 48->32 bit compressions to obtain
  285.  *    a 64-bit block (the bit numbering is given in the "CIFP" table)
  286.  *    followed by a 64->64 bit "cleanup" transformation.  (It would
  287.  *    be possible to group the bits in the 64-bit block so that 2
  288.  *    identical 32->32 bit transformations could be used instead,
  289.  *    saving a factor of 4 in space and possibly 2 in time, but
  290.  *    byte-ordering and other complications rear their ugly head.
  291.  *    Similar opportunities/problems arise in the key schedule
  292.  *    transforms.)
  293.  * PC1ROT: MSB->LSB, PC1 permutation, rotate, and PC2 permutation.
  294.  *    This admittedly baroque 64->64 bit transformation is used to
  295.  *    produce the first code (in 8*(6+2) format) of the key schedule.
  296.  * PC2ROT[0]: Inverse PC2 permutation, rotate, and PC2 permutation.
  297.  *    It would be possible to define 15 more transformations, each
  298.  *    with a different rotation, to generate the entire key schedule.
  299.  *    To save space, however, we instead permute each code into the
  300.  *    next by using a transformation that "undoes" the PC2 permutation,
  301.  *    rotates the code, and then applies PC2.  Unfortunately, PC2
  302.  *    transforms 56 bits into 48 bits, dropping 8 bits, so PC2 is not
  303.  *    invertible.  We get around that problem by using a modified PC2
  304.  *    which retains the 8 otherwise-lost bits in the unused low-order
  305.  *    bits of each byte.  The low-order bits are cleared when the
  306.  *    codes are stored into the key schedule.
  307.  * PC2ROT[1]: Same as PC2ROT[0], but with two rotations.
  308.  *    This is faster than applying PC2ROT[0] twice,
  309.  *
  310.  * The Bell Labs "salt" (Bob Baldwin):
  311.  *
  312.  * The salting is a simple permutation applied to the 48-bit result of E.
  313.  * Specifically, if bit i (1 <= i <= 24) of the salt is set then bits i and
  314.  * i+24 of the result are swapped.  The salt is thus a 24 bit number, with
  315.  * 16777216 possible values.  (The original salt was 12 bits and could not
  316.  * swap bits 13..24 with 36..48.)
  317.  *
  318.  * It is possible, but ugly, to warp the SPE table to account for the salt
  319.  * permutation.  Fortunately, the conditional bit swapping requires only
  320.  * about four machine instructions and can be done on-the-fly with about an
  321.  * 8% performance penalty.
  322.  */
  323.  
  324. typedef union {
  325.     unsigned char b[8];
  326.     struct {
  327. #if defined(LONG_IS_32_BITS)
  328.         /* long is often faster than a 32-bit bit field */
  329.         long    i0;
  330.         long    i1;
  331. #else
  332.         long    i0: 32;
  333.         long    i1: 32;
  334. #endif
  335.     } b32;
  336. #if defined(B64)
  337.     B64    b64;
  338. #endif
  339. } C_block;
  340.  
  341. /*
  342.  * Convert twenty-four-bit long in host-order
  343.  * to six bits (and 2 low-order zeroes) per char little-endian format.
  344.  */
  345. #define    TO_SIX_BIT(rslt, src) {                \
  346.         C_block cvt;                \
  347.         cvt.b[0] = src; src >>= 6;        \
  348.         cvt.b[1] = src; src >>= 6;        \
  349.         cvt.b[2] = src; src >>= 6;        \
  350.         cvt.b[3] = src;                \
  351.         rslt = (cvt.b32.i0 & 0x3f3f3f3fL) << 2;    \
  352.     }
  353.  
  354. /*
  355.  * These macros may someday permit efficient use of 64-bit integers.
  356.  */
  357. #define    ZERO(d,d0,d1)            d0 = 0, d1 = 0
  358. #define    LOAD(d,d0,d1,bl)        d0 = (bl).b32.i0, d1 = (bl).b32.i1
  359. #define    LOADREG(d,d0,d1,s,s0,s1)    d0 = s0, d1 = s1
  360. #define    OR(d,d0,d1,bl)            d0 |= (bl).b32.i0, d1 |= (bl).b32.i1
  361. #define    STORE(s,s0,s1,bl)        (bl).b32.i0 = s0, (bl).b32.i1 = s1
  362. #define    DCL_BLOCK(d,d0,d1)        long d0, d1
  363.  
  364. #if defined(LARGEDATA)
  365.     /* Waste memory like crazy.  Also, do permutations in line */
  366. #define    LGCHUNKBITS    3
  367. #define    CHUNKBITS    (1<<LGCHUNKBITS)
  368. #define    PERM6464(d,d0,d1,cpp,p)                \
  369.     LOAD(d,d0,d1,(p)[(0<<CHUNKBITS)+(cpp)[0]]);        \
  370.     OR (d,d0,d1,(p)[(1<<CHUNKBITS)+(cpp)[1]]);        \
  371.     OR (d,d0,d1,(p)[(2<<CHUNKBITS)+(cpp)[2]]);        \
  372.     OR (d,d0,d1,(p)[(3<<CHUNKBITS)+(cpp)[3]]);        \
  373.     OR (d,d0,d1,(p)[(4<<CHUNKBITS)+(cpp)[4]]);        \
  374.     OR (d,d0,d1,(p)[(5<<CHUNKBITS)+(cpp)[5]]);        \
  375.     OR (d,d0,d1,(p)[(6<<CHUNKBITS)+(cpp)[6]]);        \
  376.     OR (d,d0,d1,(p)[(7<<CHUNKBITS)+(cpp)[7]]);
  377. #define    PERM3264(d,d0,d1,cpp,p)                \
  378.     LOAD(d,d0,d1,(p)[(0<<CHUNKBITS)+(cpp)[0]]);        \
  379.     OR (d,d0,d1,(p)[(1<<CHUNKBITS)+(cpp)[1]]);        \
  380.     OR (d,d0,d1,(p)[(2<<CHUNKBITS)+(cpp)[2]]);        \
  381.     OR (d,d0,d1,(p)[(3<<CHUNKBITS)+(cpp)[3]]);
  382. #else
  383.     /* "small data" */
  384. #define    LGCHUNKBITS    2
  385. #define    CHUNKBITS    (1<<LGCHUNKBITS)
  386. #define    PERM6464(d,d0,d1,cpp,p)                \
  387.     { C_block tblk; permute(cpp,&tblk,p,8); LOAD (d,d0,d1,tblk); }
  388. #define    PERM3264(d,d0,d1,cpp,p)                \
  389.     { C_block tblk; permute(cpp,&tblk,p,4); LOAD (d,d0,d1,tblk); }
  390.  
  391. STATIC void permute(unsigned char *cp,
  392.             C_block *out,
  393.             register C_block *p,
  394.             int chars_in)
  395. {
  396.     register DCL_BLOCK(D,D0,D1);
  397.     register C_block *tp;
  398.     register int t;
  399.  
  400.     ZERO(D,D0,D1);
  401.     do {
  402.         t = *cp++;
  403.         tp = &p[t&0xf]; OR(D,D0,D1,*tp); p += (1<<CHUNKBITS);
  404.         tp = &p[t>>4];  OR(D,D0,D1,*tp); p += (1<<CHUNKBITS);
  405.     } while (--chars_in > 0);
  406.     STORE(D,D0,D1,*out);
  407. }
  408. #endif /* LARGEDATA */
  409.  
  410. /*STATIC*/ void init_des(void);
  411. STATIC void init_perm(C_block perm[64/CHUNKBITS][1<<CHUNKBITS],
  412.               unsigned char p[64],
  413.               int chars_in,
  414.               int chars_out);
  415. STATIC int des_setkey(register const char *key);
  416. STATIC int des_cipher(const char *in, char *out, long salt, int num_iter);
  417.  
  418. /* =====  (mostly) Standard DES Tables ==================== */
  419.  
  420. /* initial permutation */
  421. static const unsigned char IP[] = {
  422.     58, 50, 42, 34, 26, 18, 10,  2,
  423.     60, 52, 44, 36, 28, 20, 12,  4,
  424.     62, 54, 46, 38, 30, 22, 14,  6,
  425.     64, 56, 48, 40, 32, 24, 16,  8,
  426.     57, 49, 41, 33, 25, 17,  9,  1,
  427.     59, 51, 43, 35, 27, 19, 11,  3,
  428.     61, 53, 45, 37, 29, 21, 13,  5,
  429.     63, 55, 47, 39, 31, 23, 15,  7,
  430. };
  431.  
  432. /* The final permutation is the inverse of IP - no table is necessary */
  433.  
  434. /* expansion operation */
  435. static const unsigned char ExpandTr[] = {
  436.     32,  1,  2,  3,  4,  5,
  437.      4,  5,  6,  7,  8,  9,
  438.      8,  9, 10, 11, 12, 13,
  439.     12, 13, 14, 15, 16, 17,
  440.     16, 17, 18, 19, 20, 21,
  441.     20, 21, 22, 23, 24, 25,
  442.     24, 25, 26, 27, 28, 29,
  443.     28, 29, 30, 31, 32,  1,
  444. };
  445.  
  446. /* permuted choice table 1 */
  447. static const unsigned char PC1[] = {
  448.     57, 49, 41, 33, 25, 17,  9,
  449.      1, 58, 50, 42, 34, 26, 18,
  450.     10,  2, 59, 51, 43, 35, 27,
  451.     19, 11,  3, 60, 52, 44, 36,
  452.  
  453.     63, 55, 47, 39, 31, 23, 15,
  454.      7, 62, 54, 46, 38, 30, 22,
  455.     14,  6, 61, 53, 45, 37, 29,
  456.     21, 13,  5, 28, 20, 12,  4,
  457. };
  458.  
  459. static const unsigned char Rotates[] = {/* PC1 rotation schedule */
  460.     1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
  461. };
  462.  
  463. /* note: each "row" of PC2 is left-padded with bits that make it invertible */
  464. static const unsigned char PC2[] = {    /* permuted choice table 2 */
  465.      9, 18,    14, 17, 11, 24,  1,  5,
  466.     22, 25,     3, 28, 15,  6, 21, 10,
  467.     35, 38,    23, 19, 12,  4, 26,  8,
  468.     43, 54,    16,  7, 27, 20, 13,  2,
  469.  
  470.      0,  0,    41, 52, 31, 37, 47, 55,
  471.      0,  0,    30, 40, 51, 45, 33, 48,
  472.      0,  0,    44, 49, 39, 56, 34, 53,
  473.      0,  0,    46, 42, 50, 36, 29, 32,
  474. };
  475.  
  476. static const unsigned char S[8][64] = {    /* 48->32 bit substitution tables */
  477.                     /* S[1]            */
  478.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  479.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  480.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  481.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  482.                     /* S[2]            */
  483.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  484.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  485.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  486.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  487.                     /* S[3]            */
  488.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  489.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  490.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  491.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  492.                     /* S[4]            */
  493.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  494.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  495.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  496.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  497.                     /* S[5]            */
  498.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  499.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  500.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  501.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  502.                     /* S[6]            */
  503.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  504.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  505.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  506.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  507.                     /* S[7]            */
  508.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  509.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  510.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  511.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  512.                     /* S[8]            */
  513.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  514.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  515.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  516.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11,
  517. };
  518.  
  519. /* 32-bit permutation function */
  520. static const unsigned char P32Tr[] = {
  521.     16,  7, 20, 21,
  522.     29, 12, 28, 17,
  523.      1, 15, 23, 26,
  524.      5, 18, 31, 10,
  525.      2,  8, 24, 14,
  526.     32, 27,  3,  9,
  527.     19, 13, 30,  6,
  528.     22, 11,  4, 25,
  529. };
  530.  
  531. /* compressed/interleaved permutation */
  532. static const unsigned char CIFP[] = {
  533.      1,  2,  3,  4,   17, 18, 19, 20,
  534.      5,  6,  7,  8,   21, 22, 23, 24,
  535.      9, 10, 11, 12,   25, 26, 27, 28,
  536.     13, 14, 15, 16,   29, 30, 31, 32,
  537.  
  538.     33, 34, 35, 36,   49, 50, 51, 52,
  539.     37, 38, 39, 40,   53, 54, 55, 56,
  540.     41, 42, 43, 44,   57, 58, 59, 60,
  541.     45, 46, 47, 48,   61, 62, 63, 64,
  542. };
  543.  
  544. static const unsigned char itoa64[] =        /* 0..63 => ascii-64 */
  545.     "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  546.  
  547.  
  548. /* =====  Tables that are initialized at run time  ==================== */
  549. /* Note: these are stored into FAR section which is not duplicated */
  550.  
  551. FAR static unsigned char a64toi[128];    /* ascii-64 => 0..63 */
  552.  
  553. /* Initial key schedule permutation */
  554. FAR static C_block    PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
  555.  
  556. /* Subsequent key schedule rotation permutations */
  557. FAR static C_block    PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
  558.  
  559. /* Initial permutation/expansion table */
  560. FAR static C_block    IE3264[32/CHUNKBITS][1<<CHUNKBITS];
  561.  
  562. /* Table that combines the S, P, and E operations.  */
  563. FAR static long SPE[2][8][64];
  564.  
  565. /* compressed/interleaved => final permutation table */
  566. FAR static C_block    CF6464[64/CHUNKBITS][1<<CHUNKBITS];
  567.  
  568.  
  569. /* ==================================== */
  570.  
  571.  
  572. static C_block    constdatablock;            /* encryption constant */
  573. static char    cryptresult[1+4+4+11+1];    /* encrypted result */
  574.  
  575. /*
  576.  * Return a pointer to provided buffer consisting of the "setting"
  577.  * followed by an encryption produced by the "key" and "setting".
  578.  *
  579.  *    Code modified by Mr. andsk for MuFS compatibility.
  580.  */
  581.  
  582. SAVEDS ASM char *R_crypt(REG(a0) const char *key,
  583.              REG(a1) const char *setting)
  584. {
  585.     int i;
  586.     char buf[200];
  587.     char user[40];    /* user name */
  588.  
  589.     BPTR file = Open("AmiTCP:db/passwd", MODE_OLDFILE);
  590.     i = 0;
  591.  
  592.     if (!file) {
  593.         cryptresult[0] = '\0';
  594.         return(cryptresult);
  595.     }
  596.     if (setting[2] == '\0') {
  597.         strcpy(cryptresult,ACrypt(buf,key,R_getlogin()));
  598.         Close(file);
  599.         return(cryptresult);
  600.     }
  601.     else {    
  602.         while (FGets(file,buf,200))
  603.             if (strstr(buf,setting)) {
  604.                 while((user[i] = buf[i]) != '|')
  605.                     i++;
  606.                 user[i] = '\0';
  607.                 break;
  608.             }
  609.         Close(file);
  610.         if (i)
  611.             strcpy(cryptresult,ACrypt(buf,key,user));
  612.         else 
  613.             cryptresult[0] = '\0';
  614.         return(cryptresult);
  615.     }
  616. }
  617.  
  618. /*
  619.  * The Key Schedule, filled in by des_setkey() or setkey().
  620.  */
  621. #define    KS_SIZE    16
  622. static C_block    KS[KS_SIZE];
  623.  
  624. /*
  625.  * Set up the key schedule from the key.
  626.  */
  627. STATIC int des_setkey(register const char *key)
  628. {
  629.     register DCL_BLOCK(K, K0, K1);
  630.     register C_block *ptabp;
  631.     register int i;
  632.     FAR static short des_ready = 0;
  633.  
  634.     if (!des_ready) {
  635.         init_des();
  636.         des_ready = 1;
  637.     }
  638.  
  639.     PERM6464(K,K0,K1,(unsigned char *)key,(C_block *)PC1ROT);
  640.     key = (char *)&KS[0];
  641.     STORE(K&~0x03030303L, K0&~0x03030303L, K1, *(C_block *)key);
  642.     for (i = 1; i < 16; i++) {
  643.         key += sizeof(C_block);
  644.         STORE(K,K0,K1,*(C_block *)key);
  645.         ptabp = (C_block *)PC2ROT[Rotates[i]-1];
  646.         PERM6464(K,K0,K1,(unsigned char *)key,ptabp);
  647.         STORE(K&~0x03030303L, K0&~0x03030303L, K1, *(C_block *)key);
  648.     }
  649.     return (0);
  650. }
  651.  
  652. /*
  653.  * Encrypt (or decrypt if num_iter < 0) the 8 chars at "in" with abs(num_iter)
  654.  * iterations of DES, using the the given 24-bit salt and the pre-computed key
  655.  * schedule, and store the resulting 8 chars at "out" (in == out is permitted).
  656.  *
  657.  * NOTE: the performance of this routine is critically dependent on your
  658.  * compiler and machine architecture.
  659.  */
  660. STATIC int des_cipher(const char *in, char *out, long salt, int num_iter)
  661. {
  662.     /* variables that we want in registers, most important first */
  663. #if defined(pdp11)
  664.     register int j;
  665. #endif
  666.     register long L0, L1, R0, R1, k;
  667.     register C_block *kp;
  668.     register int ks_inc, loop_count;
  669.     C_block B;
  670.  
  671.     L0 = salt;
  672.     TO_SIX_BIT(salt, L0);    /* convert to 4*(6+2) format */
  673.  
  674. #if defined(vax) || defined(pdp11)
  675.     salt = ~salt;    /* "x &~ y" is faster than "x & y". */
  676. #define    SALT (~salt)
  677. #else
  678. #define    SALT salt
  679. #endif
  680.  
  681. #if defined(MUST_ALIGN)
  682.     B.b[0] = in[0]; B.b[1] = in[1]; B.b[2] = in[2]; B.b[3] = in[3];
  683.     B.b[4] = in[4]; B.b[5] = in[5]; B.b[6] = in[6]; B.b[7] = in[7];
  684.     LOAD(L,L0,L1,B);
  685. #else
  686.     LOAD(L,L0,L1,*(C_block *)in);
  687. #endif
  688.     LOADREG(R,R0,R1,L,L0,L1);
  689.     L0 &= 0x55555555L;
  690.     L1 &= 0x55555555L;
  691.     L0 = (L0 << 1) | L1;    /* L0 is the even-numbered input bits */
  692.     R0 &= 0xaaaaaaaaL;
  693.     R1 = (R1 >> 1) & 0x55555555L;
  694.     L1 = R0 | R1;        /* L1 is the odd-numbered input bits */
  695.     STORE(L,L0,L1,B);
  696.     PERM3264(L,L0,L1,B.b,  (C_block *)IE3264);    /* even bits */
  697.     PERM3264(R,R0,R1,B.b+4,(C_block *)IE3264);    /* odd bits */
  698.  
  699.     if (num_iter >= 0)
  700.     {        /* encryption */
  701.         kp = &KS[0];
  702.         ks_inc  = sizeof(*kp);
  703.     }
  704.     else
  705.     {        /* decryption */
  706.         num_iter = -num_iter;
  707.         kp = &KS[KS_SIZE-1];
  708.         ks_inc  = -sizeof(*kp);
  709.     }
  710.  
  711.     while (--num_iter >= 0) {
  712.         loop_count = 8;
  713.         do {
  714.  
  715. #define    SPTAB(t, i)    (*(long *)((unsigned char *)t + i*(sizeof(long)/4)))
  716. #if defined(gould)
  717.             /* use this if B.b[i] is evaluated just once ... */
  718. #define    DOXOR(x,y,i)    x^=SPTAB(SPE[0][i],B.b[i]); y^=SPTAB(SPE[1][i],B.b[i]);
  719. #else
  720. #if defined(pdp11)
  721.             /* use this if your "long" int indexing is slow */
  722. #define    DOXOR(x,y,i)    j=B.b[i]; x^=SPTAB(SPE[0][i],j); y^=SPTAB(SPE[1][i],j);
  723. #else
  724.             /* use this if "k" is allocated to a register ... */
  725. #define    DOXOR(x,y,i)    k=B.b[i]; x^=SPTAB(SPE[0][i],k); y^=SPTAB(SPE[1][i],k);
  726. #endif
  727. #endif
  728.  
  729. #define    CRUNCH(p0, p1, q0, q1)    \
  730.             k = (q0 ^ q1) & SALT;    \
  731.             B.b32.i0 = k ^ q0 ^ kp->b32.i0;        \
  732.             B.b32.i1 = k ^ q1 ^ kp->b32.i1;        \
  733.             kp = (C_block *)((char *)kp+ks_inc);    \
  734.                             \
  735.             DOXOR(p0, p1, 0);        \
  736.             DOXOR(p0, p1, 1);        \
  737.             DOXOR(p0, p1, 2);        \
  738.             DOXOR(p0, p1, 3);        \
  739.             DOXOR(p0, p1, 4);        \
  740.             DOXOR(p0, p1, 5);        \
  741.             DOXOR(p0, p1, 6);        \
  742.             DOXOR(p0, p1, 7);
  743.  
  744.             CRUNCH(L0, L1, R0, R1);
  745.             CRUNCH(R0, R1, L0, L1);
  746.         } while (--loop_count != 0);
  747.         kp = (C_block *)((char *)kp-(ks_inc*KS_SIZE));
  748.  
  749.  
  750.         /* swap L and R */
  751.         L0 ^= R0;  L1 ^= R1;
  752.         R0 ^= L0;  R1 ^= L1;
  753.         L0 ^= R0;  L1 ^= R1;
  754.     }
  755.  
  756.     /* store the encrypted (or decrypted) result */
  757.     L0 = ((L0 >> 3) & 0x0f0f0f0fL) | ((L1 << 1) & 0xf0f0f0f0L);
  758.     L1 = ((R0 >> 3) & 0x0f0f0f0fL) | ((R1 << 1) & 0xf0f0f0f0L);
  759.     STORE(L,L0,L1,B);
  760.     PERM6464(L,L0,L1,B.b, (C_block *)CF6464);
  761. #if defined(MUST_ALIGN)
  762.     STORE(L,L0,L1,B);
  763.     out[0] = B.b[0]; out[1] = B.b[1]; out[2] = B.b[2]; out[3] = B.b[3];
  764.     out[4] = B.b[4]; out[5] = B.b[5]; out[6] = B.b[6]; out[7] = B.b[7];
  765. #else
  766.     STORE(L,L0,L1,*(C_block *)out);
  767. #endif
  768.     return (0);
  769. }
  770.  
  771.  
  772. /*
  773.  * Initialize various tables.  This need only be done once.  It could even be
  774.  * done at compile time, if the compiler were capable of that sort of thing.
  775.  */
  776. void init_des(void)
  777. {
  778.     register int i, j;
  779.     register long k;
  780.     register int tableno;
  781.     static unsigned char perm[64], tmp32[32];    /* "static" for speed */
  782.  
  783.     /*
  784.      * table that converts chars "./0-9A-Za-z"to integers 0-63.
  785.      */
  786.     for (i = 0; i < 64; i++)
  787.         a64toi[itoa64[i]] = i;
  788.  
  789.     /*
  790.      * PC1ROT - bit reverse, then PC1, then Rotate, then PC2.
  791.      */
  792.     for (i = 0; i < 64; i++)
  793.         perm[i] = 0;
  794.     for (i = 0; i < 64; i++) {
  795.         if ((k = PC2[i]) == 0)
  796.             continue;
  797.         k += Rotates[0]-1;
  798.         if ((k%28) < Rotates[0]) k -= 28;
  799.         k = PC1[k];
  800.         if (k > 0) {
  801.             k--;
  802.             k = (k|07) - (k&07);
  803.             k++;
  804.         }
  805.         perm[i] = k;
  806.     }
  807. #ifdef DEBUG
  808.     prtab("pc1tab", perm, 8);
  809. #endif
  810.     init_perm(PC1ROT, perm, 8, 8);
  811.  
  812.     /*
  813.      * PC2ROT - PC2 inverse, then Rotate (once or twice), then PC2.
  814.      */
  815.     for (j = 0; j < 2; j++) {
  816.         unsigned char pc2inv[64];
  817.         for (i = 0; i < 64; i++)
  818.             perm[i] = pc2inv[i] = 0;
  819.         for (i = 0; i < 64; i++) {
  820.             if ((k = PC2[i]) == 0)
  821.                 continue;
  822.             pc2inv[k-1] = i+1;
  823.         }
  824.         for (i = 0; i < 64; i++) {
  825.             if ((k = PC2[i]) == 0)
  826.                 continue;
  827.             k += j;
  828.             if ((k%28) <= j) k -= 28;
  829.             perm[i] = pc2inv[k];
  830.         }
  831. #ifdef DEBUG
  832.         prtab("pc2tab", perm, 8);
  833. #endif
  834.         init_perm(PC2ROT[j], perm, 8, 8);
  835.     }
  836.  
  837.     /*
  838.      * Bit reverse, then initial permutation, then expansion.
  839.      */
  840.     for (i = 0; i < 8; i++) {
  841.         for (j = 0; j < 8; j++) {
  842.             k = (j < 2)? 0: IP[ExpandTr[i*6+j-2]-1];
  843.             if (k > 32)
  844.                 k -= 32;
  845.             else if (k > 0)
  846.                 k--;
  847.             if (k > 0) {
  848.                 k--;
  849.                 k = (k|07) - (k&07);
  850.                 k++;
  851.             }
  852.             perm[i*8+j] = k;
  853.         }
  854.     }
  855. #ifdef DEBUG
  856.     prtab("ietab", perm, 8);
  857. #endif
  858.     init_perm(IE3264, perm, 4, 8);
  859.  
  860.     /*
  861.      * Compression, then final permutation, then bit reverse.
  862.      */
  863.     for (i = 0; i < 64; i++) {
  864.         k = IP[CIFP[i]-1];
  865.         if (k > 0) {
  866.             k--;
  867.             k = (k|07) - (k&07);
  868.             k++;
  869.         }
  870.         perm[k-1] = i+1;
  871.     }
  872. #ifdef DEBUG
  873.     prtab("cftab", perm, 8);
  874. #endif
  875.     init_perm(CF6464, perm, 8, 8);
  876.  
  877.     /*
  878.      * SPE table
  879.      */
  880.     for (i = 0; i < 48; i++)
  881.         perm[i] = P32Tr[ExpandTr[i]-1];
  882.     for (tableno = 0; tableno < 8; tableno++) {
  883.         for (j = 0; j < 64; j++)  {
  884.             k = (((j >> 0) &01) << 5)|
  885.                 (((j >> 1) &01) << 3)|
  886.                 (((j >> 2) &01) << 2)|
  887.                 (((j >> 3) &01) << 1)|
  888.                 (((j >> 4) &01) << 0)|
  889.                 (((j >> 5) &01) << 4);
  890.             k = S[tableno][k];
  891.             k = (((k >> 3)&01) << 0)|
  892.                 (((k >> 2)&01) << 1)|
  893.                 (((k >> 1)&01) << 2)|
  894.                 (((k >> 0)&01) << 3);
  895.             for (i = 0; i < 32; i++)
  896.                 tmp32[i] = 0;
  897.             for (i = 0; i < 4; i++)
  898.                 tmp32[4 * tableno + i] = (k >> i) & 01;
  899.             k = 0;
  900.             for (i = 24; --i >= 0; )
  901.                 k = (k<<1) | tmp32[perm[i]-1];
  902.             TO_SIX_BIT(SPE[0][tableno][j], k);
  903.             k = 0;
  904.             for (i = 24; --i >= 0; )
  905.                 k = (k<<1) | tmp32[perm[i+24]-1];
  906.             TO_SIX_BIT(SPE[1][tableno][j], k);
  907.         }
  908.     }
  909. }
  910.  
  911. /*
  912.  * Initialize "perm" to represent transformation "p", which rearranges
  913.  * (perhaps with expansion and/or contraction) one packed array of bits
  914.  * (of size "chars_in" characters) into another array (of size "chars_out"
  915.  * characters).
  916.  *
  917.  * "perm" must be all-zeroes on entry to this routine.
  918.  */
  919. STATIC void init_perm(C_block perm[64/CHUNKBITS][1<<CHUNKBITS],
  920.               unsigned char p[64],
  921.               int chars_in,
  922.               int chars_out)
  923. {
  924.     register int i, j, k, l;
  925.  
  926.     for (k = 0; k < chars_out*8; k++) {    /* each output bit position */
  927.         l = p[k] - 1;        /* where this bit comes from */
  928.         if (l < 0)
  929.             continue;    /* output bit is always 0 */
  930.         i = l>>LGCHUNKBITS;    /* which chunk this bit comes from */
  931.         l = 1<<(l&(CHUNKBITS-1));    /* mask for this bit */
  932.         for (j = 0; j < (1<<CHUNKBITS); j++) {    /* each chunk value */
  933.             if ((j & l) != 0)
  934.                 perm[i][j].b[k>>3] |= 1<<(k&07);
  935.         }
  936.     }
  937. }
  938.  
  939. #if 0
  940. /*
  941.  * "setkey" routine (for backwards compatibility)
  942.  */
  943. int setkey(register const char *key)
  944. {
  945.     register int i, j, k;
  946.     C_block keyblock;
  947.  
  948.     for (i = 0; i < 8; i++) {
  949.         k = 0;
  950.         for (j = 0; j < 8; j++) {
  951.             k <<= 1;
  952.             k |= (unsigned char)*key++;
  953.         }
  954.         keyblock.b[i] = k;
  955.     }
  956.     return (des_setkey((char *)keyblock.b));
  957. }
  958.  
  959. /*
  960.  * "encrypt" routine (for backwards compatibility)
  961.  */
  962. int encrypt(register char *block, int flag)
  963. {
  964.     register int i, j, k;
  965.     C_block cblock;
  966.  
  967.     for (i = 0; i < 8; i++) {
  968.         k = 0;
  969.         for (j = 0; j < 8; j++) {
  970.             k <<= 1;
  971.             k |= (unsigned char)*block++;
  972.         }
  973.         cblock.b[i] = k;
  974.     }
  975.     if (des_cipher((char *)&cblock, (char *)&cblock, 0L, (flag ? -1: 1)))
  976.         return (1);
  977.     for (i = 7; i >= 0; i--) {
  978.         k = cblock.b[i];
  979.         for (j = 7; j >= 0; j--) {
  980.             *--block = k&01;
  981.             k >>= 1;
  982.         }
  983.     }
  984.     return (0);
  985. }
  986. #endif
  987.  
  988. #ifdef DEBUG
  989. STATIC void prtab(char *s, unsigned char *t, int num_rows)
  990. {
  991.     register int i, j;
  992.  
  993.     (void)printf("%s:\n", s);
  994.     for (i = 0; i < num_rows; i++) {
  995.         for (j = 0; j < 8; j++) {
  996.              (void)printf("%3d", t[i*8+j]);
  997.         }
  998.         (void)printf("\n");
  999.     }
  1000.     (void)printf("\n");
  1001. }
  1002. #endif
  1003.  
  1004. /****** usergroup.library/ug_GetSalt ***************************************
  1005.  
  1006.     NAME
  1007.         ug_GetSalt - generate password encryption setting for an user
  1008.  
  1009.     SYNOPSIS
  1010.         setting = ug_GetSalt(passwd, buffer, size);
  1011.           D0                   A0      A1     D0
  1012.  
  1013.         UBYTE *ug_GetSalt(const struct passwd *, UBYTE *, ULONG);
  1014.  
  1015.     FUNCTION
  1016.         This function generates a setting parameter, which is used to
  1017.         configure the password encryption process.  If an old entry exists,
  1018.         the new salt depends on it.
  1019.  
  1020.         The particular salt format depends on the system security level.
  1021.  
  1022.     INPUTS
  1023.         passwd - the old passwd entry for the user, or NULL if there is none.
  1024.  
  1025.         buffer - address of character array, which the new salt is stored
  1026.                  in.
  1027.  
  1028.         size   - the number of bytes in the buffer. The buffer should be at
  1029.                  least 12 bytes long. 
  1030.  
  1031.     RETURN VALUE
  1032.         Pointer to the buffer, or NULL if the buffer is too small.
  1033.  
  1034.     NOTE
  1035.         By default the Version 7 UNIX compatible setting is generated.
  1036.  
  1037.     SEE ALSO
  1038.        crypt()
  1039.  
  1040. ****************************************************************************
  1041. */
  1042.  
  1043. SAVEDS ASM char *R_ug_GetSalt(REG(a0) const struct passwd *pw,
  1044.                   REG(a1) char *buffer, 
  1045.                   REG(d0) ULONG size)
  1046. {
  1047.   LONG rv = LRandom();
  1048.  
  1049.   if (size < 12)
  1050.     return NULL;
  1051.  
  1052.   buffer[0] = itoa64[rv & 63];
  1053.   buffer[1] = itoa64[(rv >> 6) & 63];
  1054.   buffer[2] = '\0';
  1055.  
  1056.   return buffer;
  1057. }
  1058.